SHELL = /bin/sh

srcdir = @srcdir@
VPATH = @srcdir@

prefix=@prefix@
exec_prefix=@exec_prefix@
datarootdir=@datarootdir@
datadir=@datadir@
BINDIR=@bindir@
MANDIR=@mandir@

DUTI_BUILD_DATE=@build_date@

CC=		@CC@
FRAMEWORKS=	-framework ApplicationServices -framework CoreFoundation
OPTOPTS=	-isysroot @macosx_sdk@ \
			@macosx_arches@ \
			-mmacosx-version-min=@macosx_dep_target@ \
			@OPTOPTS@

LIBS=		@LIBS@
LDFLAGS=	@LDFLAGS@ ${LIBS}
CFLAGS=		${OPTOPTS} @CFLAGS@
INSTALL=	@INSTALL@

TARGETS=	duti
MAN1TARGETS=	duti.1
MANTARGETS=	${MAN1TARGETS}

DUTI_OBJ=	version.o util.o plist.o handler.o duti.o

all : ${TARGETS}

version.o : version.c
	${CC} ${CFLAGS} -c ${srcdir}/version.c

duti.o : duti.c
	${CC} ${CFLAGS} -c ${srcdir}/duti.c

handler.o : handler.c
	${CC} ${CFLAGS} -c ${srcdir}/handler.c

plist.o : plist.c
	${CC} ${CFLAGS} -c ${srcdir}/plist.c

util.o : util.c
	${CC} ${CFLAGS} -c ${srcdir}/util.c

duti : ${DUTI_OBJ}
	${CC} ${FRAMEWORKS} ${CFLAGS} -o duti ${DUTI_OBJ} ${LDFLAGS}

VERSION=$(shell date +%Y%m%d)
DISTDIR=duti-${VERSION}

dist : distclean
	mkdir ../${DISTDIR}
	tar -h -c -f - -X EXCLUDE . | tar xpf - -C ../${DISTDIR}
	echo ${VERSION} > ../${DISTDIR}/VERSION
	sed -e "s@INTERNAL@${VERSION}@" \
		< configure.ac > ../${DISTDIR}/configure.ac
	(cd "../${DISTDIR}"; autoconf; rm -rf autom4te.cache)
	for i in ${MANTARGETS}; do \
	    sed -e 's@_DUTI_BUILD_DATE@${DUTI_BUILD_DATE}@g' \
		    ../${DISTDIR}/$$i > ../${DISTDIR}/$$i.tmp; \
	    mv -f ../${DISTDIR}/$$i.tmp ../${DISTDIR}/$$i; \
	done
	(cd .. && tar cvfz ${DISTDIR}.tar.gz ${DISTDIR})
	for c in sha1 rmd160 md5; do \
	    (cd .. && openssl $$c ${DISTDIR}.tar.gz); \
	done

install : all
	-mkdir -p ${DESTDIR}${BINDIR}
	${INSTALL} -m 0755 -c ${TARGETS} ${DESTDIR}${BINDIR}/
	-mkdir -p ${DESTDIR}${MANDIR}/man1
	${INSTALL} -m 0644 -c ${MAN1TARGETS} ${DESTDIR}${MANDIR}/man1/

PKGNAME=	duti-${VERSION}
PKGDIR=		tmp/duti-installer
PKGRSRC=	$(wildcard pkg-resources/*.rtf)
PKGINFODIR=	tmp/Info
PKGINFO=	pkg-resources/Description.plist pkg-resources/Info.plist
DEVUTILS=	/Developer/Applications/Utilities
PKGMKR=		${DEVUTILS}/PackageMaker.app/Contents/MacOS/packagemaker

packageroot : all
	-mkdir -p -m 0755 ${PKGDIR}/${BINDIR}
	for i in ${TARGETS}; do \
	    ${INSTALL} -m 0755 -c $$i ${PKGDIR}/${BINDIR}/; \
	done
	-mkdir -p -m 0755 ${PKGDIR}/${MANDIR}
	-mkdir -p -m 0755 ${PKGDIR}/${MANDIR}/man1
	for i in ${MANTARGETS}; do \
	    ${INSTALL} -m 0644 $$i ${PKGDIR}/${MANDIR}/man1/; \
	    sed -e 's@_DUTI_BUILD_DATE@${DUTI_BUILD_DATE}@g' \
		    ${PKGDIR}/${MANDIR}/man1/$$i > \
		    ${PKGDIR}/${MANDIR}/man1/$$i.tmp; \
	    mv -f ${PKGDIR}/${MANDIR}/man1/$$i.tmp \
		    ${PKGDIR}/${MANDIR}/man1/$$i; \
	done
	-mkdir -p -m 0755 ${PKGINFODIR}
	for i in ${PKGINFO}; do \
	    sed -e 's@_DUTI_VERSION@${VERSION}@g' $$i \
			> ${PKGINFODIR}/`basename $$i`; \
	done
	for i in ${PKGRSRC}; do \
	    ${INSTALL} -m 0644 -c $$i ${PKGINFODIR}/`basename $$i`; \
	done
	sudo chown -R root:wheel ${PKGDIR}
	sudo chgrp admin ${PKGDIR}

pkgmaker : packageroot
	${PKGMKR} -build -p ../${PKGNAME}.pkg \
	    -f ${PKGDIR} -r ${PKGINFODIR} \
	    -i ${PKGINFODIR}/Info.plist \
	    -d ${PKGINFODIR}/Description.plist
	(cd .. && tar cvfz ${PKGNAME}.pkg.tar.gz ${PKGNAME}.pkg)
	sudo rm -rf tmp
	for c in sha1 rmd160 md5; do \
	    (cd ..; openssl $$c ${PKGNAME}.pkg.tar.gz); \
	done

pkgbld : packageroot
	pkgbuild --root ${PKGDIR} \
            --identifier public-domain.mortensen.duti-installer \
	    --version ${VERSION} \
	    ../${PKGNAME}.pkg

pkgbldsigned : pkgbld
	/bin/mv ../${PKGNAME}.pkg ../${PKGNAME}-unsigned.pkg
	productsign --timestamp --sign "${INST_KEY}" ../${PKGNAME}-unsigned.pkg ../${PKGNAME}.pkg

pkgcln :
	(cd .. && tar cvfz ${PKGNAME}.pkg.tar.gz ${PKGNAME}.pkg)
	sudo rm -rf tmp
	for c in sha1 rmd160 md5; do \
	    (cd ..; openssl $$c ${PKGNAME}.pkg.tar.gz); \
	done

pkg : pkgbld pkgcln

pkgsigned : pkgbldsigned pkgcln

clean :
	rm -f *.o a.out core
	rm -f ${TARGETS}
	rm -rf tmp

distclean : clean
	rm -f config.log config.status config.cache Makefile version.c
	rm -rf autom4te.cache
	rm -rf .#*
